package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkBlock;
import com.pearadmin.modules.sys.domain.dto.WorkUpdateDto;
import com.pearadmin.modules.sys.service.IProducerInfoService;
import com.pearadmin.modules.sys.service.ISysDictDataService;
import com.pearadmin.modules.sys.service.IWorkBlockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 碳块工作记录Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"碳块工作记录"})
@RequestMapping(ControllerConstant.API_WORK_PREFIX + "block")
public class WorkBlockController extends BaseController {
    private String prefix = "work/block";

    @Autowired
    private IWorkBlockService workBlockService;

    @Autowired
    private IProducerInfoService producerInfoService;

    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/block/main','work:block:main')")
    public ModelAndView main(Model model) {
        model.addAttribute("producerInfos", producerInfoService.selectProducerInfoList(null));
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询碳块工作记录列表
     */
    @ResponseBody
    @ApiOperation("碳块工作记录数据列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/block/data','work:block:data')")
    public ResultTable list(@ModelAttribute WorkBlock workBlock, PageDomain pageDomain) {
        PageInfo<WorkBlock> pageInfo = workBlockService.selectWorkBlockPage(workBlock, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增碳块工作记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/block/add','work:block:add')")
    public ModelAndView add(Model model) {
        model.addAttribute("producerInfos", producerInfoService.selectProducerInfoList(null));
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增碳块工作不合格图片信息
     */
    @GetMapping("/file/add")
    public ModelAndView fileAdd(@RequestParam("id") String blockId, Model model) {
        model.addAttribute("blockId", blockId);
        return jumpPage(prefix + "/file/addFile");
    }

    /**
     * 新增保存碳块工作记录
     */
    @ResponseBody
    @ApiOperation("新增碳块工作记录")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/block/add','work:block:add')")
    public Result save(@RequestBody WorkBlock workBlock) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workBlock.setCreateTime(LocalDateTime.now());
        workBlock.setCreateBy(sysUser.getUserId());
        workBlock.setCreateName(sysUser.getRealName());
        workBlock.setId(SequenceUtil.makeStringId());
        return decide(workBlockService.insertWorkBlock(workBlock));
    }

    /**
     * 修改碳块工作记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/block/edit','work:block:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkBlock workBlock = workBlockService.selectWorkBlockById(id);
        workBlock.setUpdateTime(LocalDateTime.now());
        mmap.put("producerInfos", producerInfoService.selectProducerInfoList(null));
        mmap.put("workBlock", workBlock);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存碳块工作记录
     */
    @ResponseBody
    @PutMapping("/update")
    @ApiOperation("修改碳块工作记录")
    @PreAuthorize("hasPermission('/work/block/edit','work:block:edit')")
    public Result update(@RequestBody WorkBlock workBlock) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workBlock.setUpdateTime(LocalDateTime.now());
        workBlock.setUpdateBy(sysUser.getUserId());
        workBlock.setUpdateName(sysUser.getUsername());
        return decide(workBlockService.updateWorkBlock(workBlock));
    }

    /**
     * 更新碳块图片信息
     * @return
     */
    @ResponseBody
    @PostMapping("/updateContent")
    public Result updateContent(@RequestBody WorkUpdateDto dto) {
        WorkBlock workBlock = workBlockService.selectWorkBlockById(dto.getId());
        workBlock.setContent(dto.getContent());
        return decide(workBlockService.updateWorkBlock(workBlock));
    }

    /**
     * 删除碳块工作记录
     */
    @ResponseBody
    @ApiOperation("批量删除碳块工作记录")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/block/remove','work:block:remove')")
    public Result batchRemove(String ids) {
        return decide(workBlockService.deleteWorkBlockByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据碳块工作记录id 删除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/work/block/remove','work:block:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workBlockService.deleteWorkBlockById(id));
    }
}
